<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Store;


use App\Api\Controllers\BaseController;
use App\Models\Store;
use App\Models\User;
use App\Services\PrintSqlService;
use App\Traits\ResponseTrait;
use App\Util\ListUtil;
use Illuminate\Http\Request;

class StoreController extends BaseController
{

    use ResponseTrait;

    public function getPStore(Request $request): string
    {
        $storeName = $request->get("store_name");
        if(empty($storeName)){
            return $this->errorResponse("请输入店铺名称");
        }
        $user = $this->parseToken();
        //获取下级代理商
        $systemUserArray = User::query()->select("id", "pid", "name")->get()->toArray();
        $systemUserArray = ListUtil::createListsNode($systemUserArray);
        $userCollection = ListUtil::getListsNode($systemUserArray, ["id" => $user->user_id]);
        $userCollection = ListUtil::listsNodeToArray([$userCollection]);
        $userCollectionIds = array_column($userCollection, "id");
        $stores = Store::query()
            ->select(["id","pid","store_id","store_name","user_id"])
            ->whereIn("user_id", $userCollectionIds)
            ->where("store_name", "like", "%" . $storeName . "%")
            ->where("pid",0)
            ->get()->toArray();

        return $this->successResponse("ok", ["data" => $stores]);
    }

}
